using System;
using System.Globalization;
using System.Text.RegularExpressions;
using UnityEngine;

namespace Voodoo.Sauce.Internal.Extension
{
	public static class StringExtension
	{
		public static int CompareVersionTo(this string currentVersion, string minimalVersion)
		{
			string[] array = currentVersion.Split('.');
			string[] array2 = minimalVersion.Split('.');
			int num = Math.Min(array.Length, array2.Length);
			for (int i = 0; i < num; i++)
			{
				try
				{
					int num2 = Convert.ToInt32(array[i]).CompareTo(Convert.ToInt32(array2[i]));
					if (num2 > 0)
					{
						return 1;
					}
					if (num2 < 0)
					{
						return -1;
					}
				}
				catch (Exception innerException)
				{
					throw new FormatException("This method is using the System.Version convention. One of your version is not in the correct format : " + array[i] + ", " + array2[i], innerException);
				}
			}
			if (array.Length < array2.Length)
			{
				for (int j = num; j < array2.Length; j++)
				{
					if (array2[j] != "0")
					{
						return -1;
					}
				}
			}
			if (array.Length > array2.Length)
			{
				for (int k = num; k < array.Length; k++)
				{
					if (array[k] != "0")
					{
						return 1;
					}
				}
			}
			return 0;
		}

		public static string RemoveVersionLastDigit(this string version)
		{
			int num = version.LastIndexOf(".");
			if (num != -1)
			{
				return version.Substring(0, num);
			}
			return version;
		}

		public static void CopyToClipboard(this string text)
		{
			TextEditor textEditor = new TextEditor();
			textEditor.text = text;
			textEditor.SelectAll();
			textEditor.Copy();
		}

		public static string BoldText(this string text)
		{
			return "<b>" + text + "</b>";
		}

		public static string RemoveSpace(this string text)
		{
			return Regex.Replace(text, "\\s+", "");
		}

		public static string Remove(this string text, string textToRemove)
		{
			return text.Replace(textToRemove, "");
		}

		public static string StringValue(this int value)
		{
			return value.ToString(CultureInfo.InvariantCulture);
		}

		public static string StringValue(this float value)
		{
			return value.ToString(CultureInfo.InvariantCulture);
		}

		public static string Capitalize(this string text)
		{
			return Regex.Replace(text, "^[a-z]", (Match match) => match.Value.ToUpperInvariant());
		}

		public static string FormatKeyName(this object obj)
		{
			return obj.ToString().ToLowerInvariant().Replace(' ', '_');
		}

		public static string Truncate(this string value, int maxChars)
		{
			if (!string.IsNullOrEmpty(value) && maxChars > 0 && value.Length > maxChars)
			{
				return value.Substring(0, maxChars) + "...";
			}
			return value;
		}
	}
}
